package net.soulwolf.test.poidemo;

import android.graphics.Bitmap;
import android.support.v4.widget.ContentLoadingProgressBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class WebActivity extends AppCompatActivity {

    public static final String KEY_HTML_URL = "key_html_url";

    private WebView mWebView;
    private WebSettings mWebSettings;
    private ProgressBar mLoadingProgress;
    private String  mHtmlUrl;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_word);
        mWebView = (WebView) findViewById(android.R.id.widget_frame);
        mLoadingProgress = (ProgressBar) findViewById(android.R.id.empty);
        if(getIntent() != null){
            mHtmlUrl = getIntent().getStringExtra(KEY_HTML_URL);
        }
        mWebSettings = mWebView.getSettings();
        initializeWebSettings();

        mWebView.setWebViewClient(new WebClient());
        mWebView.setWebChromeClient(new WebChromeClient());

        if(!TextUtils.isEmpty(mHtmlUrl)){
            mWebView.loadUrl(mHtmlUrl);
        }
    }

    /** 初始化WebSettings */
    private void initializeWebSettings() {
        mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
        mWebSettings.setAllowFileAccess(true);
        mWebSettings.setUseWideViewPort(false);
        mWebSettings.setDomStorageEnabled(true);
        mWebSettings.setDatabaseEnabled(true);
        mWebSettings.setLoadWithOverviewMode(true);
        mWebSettings.setBlockNetworkImage(false);
        mWebSettings.setBlockNetworkLoads(false);
        mWebSettings.setSupportZoom(false);
        mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
        mWebSettings.setDefaultTextEncodingName(Constants.DEFAULT_ENCODING);

    }

    class WebClient extends WebViewClient{

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            mLoadingProgress.setVisibility(View.VISIBLE);
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            mLoadingProgress.setVisibility(View.GONE);
        }
    }
}
